<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <!-- <el-form-item label="店铺名称" prop="storeManagementName">
        <el-input
          v-model="queryParams.storeManagementName"
          placeholder="请输入店铺名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="店铺注册时间" prop="storeManagementCreateTime">
        <el-date-picker clearable size="small" style="width: 200px"
          v-model="queryParams.storeManagementCreateTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择店铺注册时间">
        </el-date-picker>
      </el-form-item> -->
      <!-- <el-form-item label="店铺营业时间" prop="storeManagementBusinessHours">
        <el-date-picker clearable size="small" style="width: 200px"
          v-model="queryParams.storeManagementBusinessHours"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择店铺营业时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="店铺位置" prop="storeManagementPath">
        <el-input
          v-model="queryParams.storeManagementPath"
          placeholder="请输入店铺位置"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="店铺描述" prop="storeManagementContent">
        <el-input
          v-model="queryParams.storeManagementContent"
          placeholder="请输入店铺描述"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客服电话" prop="storeManagementPhone">
        <el-input
          v-model="queryParams.storeManagementPhone"
          placeholder="请输入客服电话"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="运费" prop="storeManagementFreight">
        <el-input
          v-model="queryParams.storeManagementFreight"
          placeholder="请输入运费"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="用户" prop="storeManagementPerid">
        <el-select v-model="queryParams.storeManagementPerid" placeholder="请选择用户"
          size="small">
          <el-option v-for="(item,index) in personageList" :key="index" :label="item.username" :value="item.uid"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="店铺状态" prop="storeManagementStatus">
        <el-select v-model="queryParams.storeManagementStatus" placeholder="请选择店铺状态" clearable size="small">
          <el-option label="未营业" :value="0" />
          <el-option label="营业中" :value="1" />
          <el-option label="暂停营业" :value="2" />
          <el-option label="已停业" :value="3" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:management:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:management:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:management:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:management:export']"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="managementList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" /><!--
      <el-table-column label="店铺标识" align="center" prop="storeManagementId" /> -->
      <el-table-column label="店铺logo" align="center" prop="storeManagementLogo" >
        <template style="width: 100px; height: 100px;border-radius: 50%;"
         slot-scope="scope" v-if="scope.row.storeManagementLogo != null && scope.row.storeManagementLogo != ''">
        	<el-popover trigger="hover" placement="top">
        		<img v-for="(item, index) in scope.row.storeManagementLogo.split(',')" style="width: 100px;margin-left: 5px;" :key="index" :src="item" />
        		<div slot="reference" class="name-wrapper"><el-tag size="medium">查看</el-tag></div>
        	</el-popover>
        </template>
      </el-table-column>
      <el-table-column label="店铺展示图" align="center" prop="storeManagementImages" >
        <template style="width: 100px; height: 100px;border-radius: 50%;"
         slot-scope="scope" v-if="scope.row.storeManagementImages != null && scope.row.storeManagementImages != ''">
        	<el-popover trigger="hover" placement="top">
        		<img v-for="(item, index) in scope.row.storeManagementImages.split(',')" style="width: 100px;margin-left: 5px;" :key="index" :src="item" />
        		<div slot="reference" class="name-wrapper"><el-tag size="medium">查看</el-tag></div>
        	</el-popover>
        </template>
      </el-table-column>
      <el-table-column label="店铺名称" align="center" prop="storeManagementName" />
      <el-table-column label="店铺注册时间" align="center" prop="storeManagementCreateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.storeManagementCreateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="店铺营业时间" align="center" prop="storeManagementBusinessHours" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.storeManagementBusinessHours, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="店铺位置" align="center" prop="storeManagementPath" :show-overflow-tooltip="true"/>
      <el-table-column label="店铺描述" align="center" prop="storeManagementContent" :show-overflow-tooltip="true"/>
      <el-table-column label="客服电话" align="center" prop="storeManagementPhone" />
      <el-table-column label="运费(元)" align="center" prop="storeManagementFreight" >
        <template slot-scope="scope">
          {{scope.row.storeManagementFreight+'元'}}
        </template>
      </el-table-column>
      <el-table-column label="用户" align="center" prop="storeManagementPerid" >
        <template slot-scope="scope" >
          {{scope.row.personage!= null ? scope.row.personage.username :'暂无'}}
        </template>
      </el-table-column>
      <el-table-column label="店铺状态" align="center" prop="storeManagementStatus" >
        <template slot-scope="scope"><!--  0未营业 1营业中 2暂停营业 3已停业 -->
          {{scope.row.storeManagementStatus == 0 ? '未营业':(scope.row.storeManagementStatus == 1 ? '营业中':(scope.row.storeManagementStatus == 2 ? '暂停营业':'已停业'))}}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:management:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:management:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改【请填写功能名称】对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="店铺logo" prop="storeManagementLogo">
          <!-- <el-input v-model="form.storeManagementLogo" type="textarea" placeholder="请输入内容" /> -->
          <el-upload
              action="https://jsonplaceholder.typicode.com/posts/"
            	:auto-upload="false"
            	ref="upload"
            	list-type="picture-card"
            	:http-request="uploadFile"
            	:limit="1"
            	:on-preview="handlePictureCardPreview"
            	:on-remove="handleRemove"
            	:on-success="imageChange"
            	:file-list="imageList"
            ></el-upload>
            <el-dialog :visible.sync="dialogVisible"><img width="100%" :src="dialogImageUrl" alt="" /></el-dialog>
        </el-form-item>
        <el-form-item label="店铺展示图" prop="storeManagementImages"><!--
          <el-input v-model="form.storeManagementImages" type="textarea" placeholder="请输入内容" /> -->
          <el-upload
              action="https://jsonplaceholder.typicode.com/posts/"
            	:auto-upload="false"
            	ref="uploads"
            	list-type="picture-card"
            	:http-request="uploadFiles"
            	:limit="5"
            	:on-preview="handlePictureCardPreviews"
            	:on-remove="handleRemoves"
            	:on-success="imageChanges"
            	:file-list="imageLists"
            ></el-upload>
            <el-dialog :visible.sync="dialogVisibles"><img width="100%" :src="dialogImageUrls" alt="" /></el-dialog>
        </el-form-item>
        <el-form-item label="店铺名称" prop="storeManagementName">
          <el-input v-model="form.storeManagementName" placeholder="请输入店铺名称" />
        </el-form-item><!--
        <el-form-item label="店铺注册时间" prop="storeManagementCreateTime">
          <el-date-picker clearable size="small" style="width: 100%"
            v-model="form.storeManagementCreateTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择店铺注册时间">
          </el-date-picker>
        </el-form-item> -->
        <el-form-item label="店铺营业时间" prop="storeManagementBusinessHours">
          <el-date-picker clearable size="small" style="width: 100%"
            v-model="form.storeManagementBusinessHours"
            type="date"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择店铺营业时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="店铺位置" prop="storeManagementPath">
          <el-input v-model="form.storeManagementPath" placeholder="请输入店铺位置" />
        </el-form-item>
        <el-form-item label="店铺描述" prop="storeManagementContent">
          <el-input type="textarea" v-model="form.storeManagementContent" placeholder="请输入店铺描述" />
        </el-form-item>
        <el-form-item label="客服电话" prop="storeManagementPhone">
          <el-input v-model="form.storeManagementPhone" placeholder="请输入客服电话" />
        </el-form-item>
        <el-form-item label="运费" prop="storeManagementFreight">
          <el-input v-model="form.storeManagementFreight" placeholder="请输入运费" >
            <template slot="append">元</template>
          </el-input>
        </el-form-item>
        <el-form-item label="用户" prop="storeManagementPerid"><!--
          <el-input v-model="form.storeManagementPerid" placeholder="请输入用户—外键" /> -->
          <el-select v-model="form.storeManagementPerid" placeholder="请选择用户" style="width: 100%">
            <el-option v-for="(item,index) in personageList" :key="index" :label="item.username" :value="item.uid"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="店铺状态">
          <el-select v-model="form.storeManagementStatus" placeholder="请选择店铺状态" style="width: 100%">
            <el-option label="未营业" :value="0"></el-option>
            <el-option label="营业中" :value="1"></el-option>
            <el-option label="暂停营业" :value="2"></el-option>
            <el-option label="已停业" :value="3"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listManagement, getManagement, delManagement, addManagement, updateManagement, exportManagement, uploadImgs } from "@/api/system/storeManagement";
import { listPersonage } from "@/api/system/personage";
export default {
  name: "Management",
  data() {
    return {
      //开始
      imgurl:process.env.VUE_APP_BASE_API,
      formDate: null,
      uploadList: [],
      dialogImageUrl: '',
      dialogVisible: false,
      imageList: [],
      //结束
      //开始
      imgurls:process.env.VUE_APP_BASE_API,
      formDates: null,
      uploadLists: [],
      dialogImageUrls: '',
      dialogVisibles: false,
      imageLists: [],
      //结束
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      managementList: [],
      // 用户集合
      personageList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        storeManagementLogo: undefined,
        storeManagementImages: undefined,
        storeManagementName: undefined,
        storeManagementCreateTime: undefined,
        storeManagementBusinessHours: undefined,
        storeManagementPath: undefined,
        storeManagementContent: undefined,
        storeManagementPhone: undefined,
        storeManagementFreight: undefined,
        storeManagementPerid: undefined,
        storeManagementStatus: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        storeManagementLogo: [
          { required: true, message: "店铺logo不能为空", trigger: "blur" }
        ],
        storeManagementImages: [
          { required: true, message: "店铺展示图不能为空", trigger: "blur" }
        ],
        storeManagementName: [
          { required: true, message: "店铺名称不能为空", trigger: "blur" }
        ],
        storeManagementBusinessHours: [
          { required: true, message: "店铺营业时间不能为空", trigger: "blur" }
        ],
        storeManagementPath: [
          { required: true, message: "店铺位置不能为空", trigger: "blur" }
        ],
        storeManagementContent: [
          { required: true, message: "店铺描述不能为空", trigger: "blur" }
        ],
        storeManagementPhone: [
          { required: true, message: "客服电话不能为空", trigger: "blur" }
        ],
        storeManagementFreight: [
          { required: true, message: "运费不能为空", trigger: "blur" }
        ],
        storeManagementPerid: [
          { required: true, message: "用户不能为空", trigger: "blur" }
        ],
        storeManagementStatus: [
          { required: true, message: "店铺状态不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getPersonageList();
  },
  methods: {
    getPersonageList(){
      listPersonage().then(response =>{
        this.personageList = response.rows;
      })
    },
    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true;
      listManagement(this.queryParams).then(response => {
        this.managementList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.imageList = [];
      this.imageLists = [];
      this.form = {
        storeManagementId: undefined,
        storeManagementLogo: undefined,
        storeManagementImages: undefined,
        storeManagementName: undefined,
        storeManagementCreateTime: undefined,
        storeManagementBusinessHours: undefined,
        storeManagementPath: undefined,
        storeManagementContent: undefined,
        storeManagementPhone: undefined,
        storeManagementFreight: undefined,
        storeManagementPerid: undefined,
        storeManagementStatus: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.storeManagementId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加店铺信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const storeManagementId = row.storeManagementId || this.ids
      getManagement(storeManagementId).then(response => {
        if (response.data.storeManagementLogo != null && response.data.storeManagementLogo != '') {/* 判断图片的地址是否为空 */
        	var imageList = response.data.storeManagementLogo.split(',');
        	for (var i = 0; i < imageList.length; i++) {
        		imageList[i] = imageList[i];
        		this.imageList.push({
        			name: imageList[i],
        			url: imageList[i]
        		});/*
            alert(imageList+"：查询显示的路径"); */
        	}
        }
        if (response.data.storeManagementImages != null && response.data.storeManagementImages != '') {/* 判断图片的地址是否为空 */
        	var imageList = response.data.storeManagementImages.split(',');
        	for (var i = 0; i < imageList.length; i++) {
        		imageList[i] = imageList[i];
        		this.imageLists.push({
        			name: imageList[i],
        			url: imageList[i]
        		});/*
            alert(imageList+"：查询显示的路径"); */
        	}
        }
        this.form = response.data;
        this.open = true;
        this.title = "修改店铺信息";
      });
    },
    /** 提交按钮 */
    submitForm: function() {/* storeManagementLogo */
      this.formDate = new FormData();//图片
      this.$refs.upload.submit();
      console.log(this.formDate);
      if (this.uploadList.length > 0) {
      	this.uploadList = [];
      	uploadImgs(this.formDate).then(responses => {
      		this.formDate = null;
      		if (responses.code === 200) {
      			this.$refs.upload.clearFiles();//清除上传列表
      			var imageList = [];
      			if (this.imageList != null) {
      				for (var i = 0; i < this.imageList.length; i++) {
      					if (this.imageList[i] != null) {
      						imageList.push(this.imageList[i].name);
      					}
      				}
      			}
      			imageList = imageList.concat(responses.data);
            console.log(responses.data);
      			this.form.storeManagementLogo = imageList.join(',');
      			this.submitForms();
      		} else {
      			this.msgError(responses.msg);
      		}
      	});
      } else {
      	this.formDate = null;
      	var imageList = [];
      	if (this.imageList != null) {
      		for (var i = 0; i < this.imageList.length; i++) {
      			if (this.imageList[i] != null) {
      				imageList.push(this.imageList[i].name);
      			}
      		}
      	}
        console.log(imageList);
      	this.form.storeManagementLogo = imageList.join(',');
      	this.submitForms();
      }

    },submitForms:function(){
      this.formDates = new FormData();//图片
      this.$refs.uploads.submit();
      if (this.uploadLists.length > 0) {
      	this.uploadLists = [];
      	uploadImgs(this.formDates).then(responses => {
      		this.formDates = null;
      		if (responses.code === 200) {
      			this.$refs.upload.clearFiles();//清除上传列表
      			var imageList = [];
      			if (this.imageLists != null) {
      				for (var i = 0; i < this.imageLists.length; i++) {
      					if (this.imageLists[i] != null) {
      						imageList.push(this.imageLists[i].name);
      					}
      				}
      			}
      			imageList = imageList.concat(responses.data);
      			this.form.storeManagementImages = imageList.join(',');
      			this.submitFormse();
      		} else {
      			this.msgError(responses.msg);
      		}
      	});
      } else {
      	this.formDates = null;
      	var imageList = [];
      	if (this.imageLists != null) {
      		for (var i = 0; i < this.imageLists.length; i++) {
      			if (this.imageLists[i] != null) {
      				imageList.push(this.imageLists[i].name);
      			}
      		}
      	}
      	this.form.storeManagementImages = imageList.join(',');
      	this.submitFormse();
      }

    },submitFormse:function(){
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.storeManagementId != undefined) {
            updateManagement(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addManagement(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const storeManagementIds = row.storeManagementId || this.ids;
      this.$confirm('是否确认删除店铺信息编号为"' + storeManagementIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delManagement(storeManagementIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有店铺信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportManagement(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    },
    /** 图片操作 */
    handleRemove(file, fileList) {//删除时
    	for (var i = 0; i < this.imageList.length; i++) {
    		if (this.imageList[i].url == file.url) {
    			this.imageList.splice(i, 1);
    		}
    	}
    },
    handlePictureCardPreview(file) {//上传文件时
      console.log(file.url);
    	this.dialogImageUrl = file.url;
    	this.dialogVisible = true;
    },
    imageChange(event, file, fileList) {//成功后
     /* alert("123成功"); */
    	console.log(event);
    	console.log(file);
    	console.log(fileList);
    },
    uploadFile(file) {//上传行为
    	this.uploadList.push(file.file);
    	this.formDate.append('file', file.file);
      console.log(this.formDate.file);
    },

    /** 图片操作 */
    handleRemoves(file, fileList) {//删除时
    	for (var i = 0; i < this.imageLists.length; i++) {
    		if (this.imageLists[i].url == file.url) {
    			this.imageLists.splice(i, 1);
    		}
    	}
    },
    handlePictureCardPreviews(file) {//上传文件时
      console.log(file.url);
    	this.dialogImageUrls = file.url;
    	this.dialogVisibles = true;
    },
    imageChanges(event, file, fileList) {//成功后
     /* alert("123成功"); */
    	console.log(event);
    	console.log(file);
    	console.log(fileList);
    },
    uploadFiles(file) {//上传行为
    	this.uploadLists.push(file.file);
      console.log(file.file);
    	this.formDates.append('file', file.file);
      console.log(this.formDates.file);
    }
  }
};
</script>
